using System;
using EasyDapr.Infrastructure;
using EasyDapr.Infrastructure.Attribute;
using EasyDapr.Infrastructure.Auth;
using EasyDapr.Infrastructure.Dapr;
using EasyDapr.Infrastructure.EfCore;
using EasyDapr.Infrastructure.Middlewares;
using EasyDapr.Infrastructure.OTel;
using EasyDapr.Infrastructure.Validator;
using InventoryService.Infrastructure.Data;
using InventoryService.Infrastructure.PubSub;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;

namespace InventoryService.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddHttpContextAccessor()
                .AddCustomMediatR<Anchor>()
                .AddCustomValidators<Anchor>()
                .AddCustomAuth<Anchor>(Configuration)
                .AddCustomDbContext<MainDbContext, Anchor>(Configuration.GetConnectionString("mysql"))
                .AddCustomDaprComponent<Anchor>()
                .AddCustomDaprClient()
                .AddControllers()
                .AddDapr();

            // services.AddHealthChecks().add

            // services.AddTransient<IDaprPublisher, DaprInventoryCreatedPublisher>();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {Title = "InventoryService.Api", Version = "v1"});
            });

            services.AddCustomOTelWithZipkin(Configuration, o =>
            {
                Console.WriteLine($"ZipkinUrl:http://{Configuration.GetSection("OTelZipkin")["Endpoint"]}");
                // o.Endpoint = o.Endpoint;
                o.Endpoint = new Uri($"{Configuration.GetSection("OTelZipkin")["Endpoint"]}");
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "InventoryService.Api v1"));
            }

            app.UseCloudEvents();

            // app.UseHttpsRedirection();
            // app.UseExceptionHandling();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                // endpoints.MapHealthChecks("/healthz", new HealthCheckOptions { Predicate = _ => true });
                // endpoints.MapHealthChecks("/liveness", new HealthCheckOptions { Predicate = r => r.Name.Contains("self") });
                endpoints.MapSubscribeHandler();
                endpoints.MapControllers();
            });

            // app.ApplicationServices.CreateLoggerConfiguration(false);
        }
    }
}